﻿using System.Diagnostics;

namespace Ensemble_Windows.Core
{
    /// <summary>
    ///     时间类
    /// </summary>
    public class Time
    {
        /// <summary>
        ///     Construct
        /// </summary>
        public Time()
        {
            Stopwatch = new Stopwatch();
        }

        private Stopwatch Stopwatch { get; set; }
        private double LastUpdate { get; set; }

        /// <summary>
        ///     时间标量
        /// </summary>
        public double ElapseTime
        {
            get { return Stopwatch.ElapsedMilliseconds * 0.001; }
        }

        /// <summary>
        ///     开始或继续时钟
        /// </summary>
        public void Start()
        {
            Stopwatch.Start();
            LastUpdate = 0;
        }

        /// <summary>
        ///     停止时钟
        /// </summary>
        public void Stop()
        {
            Stopwatch.Stop();
        }

        /// <summary>
        ///     时钟更新
        /// </summary>
        /// <returns></returns>
        public double Update()
        {
            var now = ElapseTime;
            var updateTime = now - LastUpdate;
            LastUpdate = now;
            return updateTime;
        }
    }
}